{% template "admin/header.html" %}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>编辑</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" lay-filter="form">
        <div class="layui-form-item">
            <label class="layui-form-label">所属用户组</label>
            <div class="layui-input-6" id="groups">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-4">
                <input type="text" name="user_name" autocomplete="off" lay-verify="required" placeholder="登录用户名" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                用户名在4到25个字符之间。
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-4">
                <input type="password" name="password" autocomplete="off" placeholder="登录密码" {if condition="ACTION_NAME eq 'adminadd'"}lay-verify="required"{/if} class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                密码必须大于6位，小于15位。
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">头像</label>
            <input type="hidden" name="head" id="avatar">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-primary" id="adBtn"><i class="icon icon-upload3"></i>点击上传</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="adPic">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="hidden" name="id">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">提交</button>
                <a href="javascript:window.history.back();" class="layui-btn layui-btn-primary">返回</a>
            </div>
        </div>
    </form>
</div>

{% template "admin/footer.html" %}
<script>

    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return decodeURI(r[2]); return null; //返回参数值
    }
    let admin_id = getUrlParam("id");
    layui.use(['form', 'layer','upload'], function () {
        var form = layui.form, layer = layui.layer,$= layui.jquery,upload = layui.upload;

        $.post('/admin/admin/get',{id:admin_id},function (res) {
            $.post('/admin/group/query',function (data) {
                $.each(data.Data,function (k,v) {

                    let isCheck = '';
                    if(res.Data.group.length>0){
                        for (let i=0;i<res.Data.group.length;i++){
                            if(res.Data.group[i].Group_id == v.Id){
                                isCheck = 'checked';
                            }
                        }
                    }
                    $('#groups').append('<input type="checkbox" name="group_id" value="'+v.Id+'" title="'+v.Group_name+'" '+isCheck+'>');

                });

                //form.val("form", res.data);

                $('input[name="user_name"]').val(res.Data.admin.User_name);
                $('input[name="id"]').val(res.Data.admin.Id);
                $('input[name="head"]').val(res.Data.admin.Head);
                $('#adPic').attr('src',res.Data.admin.Head);

                form.render();
            });


        });


        form.on('submit(submit)', function (data) {

            let ids = [];
            $("input[name='group_id']:checked").each(function () {
                ids.push($(this).val())
            });
            if(ids.length<=0){
                layer.msg("选择用户组");
                return;
            }
            data.field.group_id = ids.join(',');

            loading =layer.load(1, {shade: [0.1,'#fff']});
            $.post("/admin/admin/edit", data.field, function (res) {
                layer.close(loading);
                if (res.Code > 0) {
                    layer.msg(res.Msg, {time: 1800, icon: 1}, function () {
                        window.history.back();
                    });
                } else {
                    layer.msg(res.Msg, {time: 1800, icon: 2});
                }
            });
        });
        //普通图片上传
         var uploadInst = upload.render({
             elem: '#adBtn'
             ,url: '/upload/file'
             ,data:{}
             ,before: function(obj){
                 //预读本地文件示例，不支持ie8
                 obj.preview(function(index, file, result){
                     $('#adPic').attr('src', result); //图片链接（base64）
                 });
             },
             done: function(res){
                 if(res.Code>0){
                     $('#avatar').val(res.Data);
                 }else{
                     //如果上传失败
                     return layer.msg('上传失败');
                 }
             }
             ,error: function(){
                 //演示失败状态，并实现重传
                 var demoText = $('#demoText');
                 demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                 demoText.find('.demo-reload').on('click', function(){
                     uploadInst.upload();
                 });
             }
         });
    });
</script>
</body>
</html>
